﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.XPath;
using System.Xml;
using System.IO;
namespace Workshop
{
    public partial class FormTAarrangement : Form
    {
        public String ChangeTa2; 
        public String ChangeTa;  //Both ChangeTa and ChangtaTa2 is used to store the past Ta, so i can know if the user is going to swap the ta.
        public String CourseID;
      //  public String repeat1;
       // public String repeat2;
        public FormTAarrangement()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void FormTAarrangement_Load(object sender, EventArgs e)
        {
            UpdateGridView();
            UpdateComboList1();
            UpdataComboList2();
        }//Every time load the program, update the newest xml file into it.

        public void UpdataComboList2()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Staff.xml");
            String path = "//HeadTAID";
            XmlNodeList nodes = doc.SelectNodes(path);
            DataTable dt = new DataTable();
            DataColumn col1 = new DataColumn("TA", typeof(string));
            DataColumn col2 = new DataColumn("value", typeof(string));
            dt.Columns.Add(col1);
            dt.Columns.Add(col2);
            DataRow dr1 = dt.NewRow();
            dr1[0] = "";
            dr1[1] = "Reset";
            dt.Rows.Add(dr1);
            for (int i = 0; i < nodes.Count; i++)
            {
                DataRow dr2 = dt.NewRow();
                dr2[0] = nodes[i].InnerText;
                dr2[1] = nodes[i].Attributes[0].Value.ToString();
                dt.Rows.Add(dr2);
            }
            comboBox2.ValueMember = "TA";
            comboBox2.DisplayMember = "value";
            comboBox2.DataSource = dt;
        }//Here i open a specific xml file called Staff.xml to get all the TA information, and put them into a component named combolist.

        public void UpdateComboList1()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Staff.xml");
            String path = "//HeadTAID";
            XmlNodeList nodes = doc.SelectNodes(path);
            DataTable dt = new DataTable();
            DataColumn col1 = new DataColumn("TA", typeof(string));
            DataColumn col2 = new DataColumn("value", typeof(string));
            dt.Columns.Add(col1);
            dt.Columns.Add(col2);
            DataRow dr1 = dt.NewRow();
            dr1[0] = "";
            dr1[1] = "Reset";
            dt.Rows.Add(dr1);
            for (int i = 0; i < nodes.Count; i++)
            {
                DataRow dr2 = dt.NewRow();
                dr2[0] = nodes[i].InnerText;
                dr2[1] = nodes[i].Attributes[0].Value.ToString();
                dt.Rows.Add(dr2);
            }
            comboBox1.DisplayMember = "value";
            comboBox1.ValueMember = "TA";
            comboBox1.DataSource = dt;
        } //Same procedure like UpdateComboList2().

        public static void SetIDWid(DataGridView dataGridView1)
        {
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                if (dataGridView1.Columns[i].HeaderCell.Value.ToString() == "ID") //check the column name if it is "ID"
                {
                    dataGridView1.Columns[i].Width = 90;
                    dataGridView1.Columns[i].Resizable = DataGridViewTriState.False;
                    break;
                }
            }
        }//here i specify the width of a specific column in the gridView.

        public void UpdateGridView()
        {
            XmlDocument doc1 = new XmlDocument();
            XmlDocument doc2 = new XmlDocument();
            doc1.Load("Course.xml");
            doc2.Load("Staff.xml");
            String PathCourse = "//Courses/CourseInfo/Course/TAID1";
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
            DataColumn col0 = new DataColumn("ID", typeof(string));
            DataColumn col1 = new DataColumn("Course", typeof(string));
            DataColumn col2 = new DataColumn("TA", typeof(string));
            DataColumn colTA1 = new DataColumn("TAID1", typeof(string));
            DataColumn colTA2 = new DataColumn("TAID2", typeof(string));
            DataColumn col3 = new DataColumn("TA", typeof(string));
            DataColumn col4 = new DataColumn("Workload", typeof(string));
            dt1.Columns.Add(col0);
            dt1.Columns.Add(col1);
            dt1.Columns.Add(col2);
            dt1.Columns.Add(colTA1);
            dt1.Columns.Add(colTA2);
            dt2.Columns.Add(col3);
            dt2.Columns.Add(col4); //Update some information for related datatable.
            XmlNodeList Course = doc1.SelectNodes(PathCourse);
            int i = 0;
            while (i<Course.Count)
            {
                DataRow dr1 = dt1.NewRow();
                dr1[0] = Course[i].ParentNode.ChildNodes[0].InnerText;
                dr1[1] = Course[i].ParentNode.ChildNodes[1].InnerText;
                if (Course[i].ParentNode.ChildNodes[5].Attributes[0].Value.ToString() != "")//check if there has two TA for a course.
                {
                    dr1[2] = Course[i].Attributes[0].Value.ToString() + "/" + Course[i].ParentNode.ChildNodes[5].Attributes[0].Value.ToString();
                    dr1[3] = Course[i].InnerText;
                    dr1[4] = Course[i].ParentNode.ChildNodes[5].InnerText;
                } //if it has two Ta I use "/" to divide them
                else
                {
                    dr1[2] = Course[i].Attributes[0].Value.ToString();
                    dr1[3] = Course[i].InnerText;
                    dr1[4] = Course[i].ParentNode.ChildNodes[5].InnerText;
                }//else only put the information into the dataRow.
                dt1.Rows.Add(dr1);
                i++;
            }
            String path2 = "//HeadTAID";
            XmlNodeList nodes2 = doc2.SelectNodes(path2);
            for (int a = 0; a < nodes2.Count; a++)
            {
                DataRow dr2 = dt2.NewRow();
                dr2[0] = nodes2[a].Attributes[0].Value.ToString();
                dr2[1] = nodes2[a].Attributes[1].Value.ToString();
                dt2.Rows.Add(dr2);
            } //this loop is used to put all Ta information into a datatable, then just put it into a datagridView.
            dataGridView1.DataSource = dt1;
            dataGridView2.DataSource = dt2;
            SetIDWid(dataGridView1); //call function to set the width of "ID" column.
            CourseBox.Text = ""; //then reset the content of this box.
            TaBox.Text = ""; //then reset the content of this box.
           // this.dataGridView1.Columns["TAID1"].Visible = false;
            //this.dataGridView1.Columns["TAID2"].Visible = false;
        }

        private void courseTimeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCoursetime form = new FormCoursetime();
            this.Hide();
            form.Show();
        } //jump to another page.

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            CourseBox.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            String  together=dataGridView1.CurrentRow.Cells[2].Value.ToString();
            //use to consider if String needs to divide into two parts
            ChangeTa = dataGridView1.CurrentRow.Cells[3].Value.ToString(); //public variable, to check if the Tabox has content or not.
            ChangeTa2 = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            CourseID = dataGridView1.CurrentRow.Cells[0].Value.ToString(); //every cellclick store the course ID, because maybe there are some changes need to be done on it.
            TaBox.Text = ChangeTa;
            Tabox2.Text = ChangeTa2;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            String path = "//CourseID";
            XmlNodeList nodes = doc.SelectNodes(path);
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].InnerText == CourseID)
                {
                    if (nodes[i].Attributes[0].Value != "" && nodes[i].Attributes[1].Value != "" && nodes[i].Attributes[2].Value != "")
                    {
                        TaBox.Text = comboBox1.SelectedValue.ToString();
                        okbutton.Visible = true;
                    }
                    else 
                    {
                        MessageBox.Show("please input the time for the course first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        okbutton.Visible = false;
                        return;
                    }
                }
            }
               
        }
        public void CreateNewTA2(String Ta) //here, because the structure of our xml, i have to create CreateNewTA2() and CreateNewTA1() to store the Ta information into correlated coures.
        {
            int numTA=0;
            String WorkLoad;
            String TaName="";
            int workload=0;
            XmlDocument doc = new XmlDocument();
            doc.Load("Staff.xml"); //In this file have every staffs' information including workload. So every time change Ta I need to change the information of it.
            String path = "//HeadTAID";  //find the corresponding Ta.
            XmlNodeList nodess = doc.SelectNodes(path);     //
            for (int i = 0; i < nodess.Count; i++) 
            {
                if (nodess[i].InnerText == Ta)
                {
                    numTA = i;
                    WorkLoad = nodess[i].Attributes[1].Value.ToString();
                    int.TryParse(WorkLoad, out workload); 
                    TaName = nodess[i].Attributes[0].Value.ToString();
                }
            }
            //these five procedure is used to get the workload of the TA you pass into this function.
            if (workload == 6)
            {
                MessageBox.Show("the courses of this ta " + TaName + ": is 6. No more courses", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else //if the workload of that TA is smaller than 6, then the following procedure happens
            {
                workload = workload + 1;
                XmlDocument doc1 = new XmlDocument();
                doc1.Load("Course.xml");
                String path1 = "//Courses/CourseInfo/Course/Subject";
                XmlNodeList nodes = doc1.SelectNodes(path1);
                for (int i = 0; i < nodes.Count; i++)
                {
                    if (nodes[i].ParentNode.ChildNodes[0].InnerText == CourseID)
                    {
                        nodes[i].ParentNode.ChildNodes[5].InnerText = Ta;
                        nodes[i].ParentNode.ChildNodes[5].Attributes[0].Value = TaName;
                    }
                }      //here, firstly, i need to find the correlation course, and then i need to update the Ta informaion of it.
                doc1.Save("Course.xml");
                nodess[numTA].Attributes[1].Value = workload.ToString();  //finally, i add one to the workload of that Ta, and then update the correlated information in Staff.xml.
                doc.Save("Staff.xml");
            }
        }

        public void CreateNewTA(String Ta) //just used to create new Ta for a teacher.
        {
            int numTA = 0;
            String WorkLoad;
            String TaName = "";
            int workload = 0;
            XmlDocument doc = new XmlDocument();
            doc.Load("Staff.xml"); //In this file have every staffs' information including workload. So every time change Ta I need to change the information of it.
            String path = "//HeadTAID";  //find the corresponding Ta.
            XmlNodeList nodess = doc.SelectNodes(path);     //
            for (int i = 0; i < nodess.Count; i++)
            {
                if (nodess[i].InnerText == Ta)
                {
                    numTA = i;
                    WorkLoad = nodess[i].Attributes[1].Value.ToString();
                    int.TryParse(WorkLoad, out workload);
                    TaName = nodess[i].Attributes[0].Value.ToString();
                }
            }
            //these five procedure is used to get the workload of the TA you pass into this function.
            if (workload == 6)
            {
                MessageBox.Show("the courses of this ta " + TaName + ": is 6. No more courses", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else //if the workload of that TA is smaller than 6, then the following procedure happens
            {
                workload = workload + 1;
                XmlDocument doc1 = new XmlDocument();
                doc1.Load("Course.xml");
                String path1 = "//Courses/CourseInfo/Course/Subject";
                XmlNodeList nodes = doc1.SelectNodes(path1);
                for (int i = 0; i < nodes.Count; i++)
                {
                    if (nodes[i].ParentNode.ChildNodes[0].InnerText == CourseID)
                    {
                        nodes[i].ParentNode.ChildNodes[4].InnerText = Ta;
                        nodes[i].ParentNode.ChildNodes[4].Attributes[0].Value = TaName;
                    }
                }      //here, firstly, i need to find the correlation course, and then i need to update the Ta informaion of it.
                doc1.Save("Course.xml");
                nodess[numTA].Attributes[1].Value = workload.ToString();  //finally, i add one to the workload of that Ta, and then update the correlated information in Staff.xml.
                doc.Save("Staff.xml");
            }
        }  //this function is as same as CreateNewTA2().
           //but does it need to call this funcion, we still need to conduct more consideration. which you can see in the below funciton which is addTa().
        public void addTa()
        {
            if (CourseBox.Text.Trim() != String.Empty)
            {
                if (ChangeTa == "" && TaBox.Text != "") //changeTa is null but TaBox.Text!="" means we provide a Ta for the course.
                {
                    CreateNewTA(TaBox.Text);
                }
                else if (ChangeTa != "" && TaBox.Text != ""&&ChangeTa !=TaBox.Text)  //means HeadTa want to change another Ta for the same course.
                {
                    int workload;
                    XmlDocument minusWorkLoad = new XmlDocument();
                    minusWorkLoad.Load("Staff.xml");
                    String PathForMinus = "//HeadTAID";
                    XmlNodeList nodes = minusWorkLoad.SelectNodes(PathForMinus);
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        if (nodes[i].InnerText == ChangeTa)
                        {
                            int.TryParse(nodes[i].Attributes[1].Value, out workload);
                            workload = workload - 1;
                            nodes[i].Attributes[1].Value = workload.ToString();
                        }
                    }
                    minusWorkLoad.Save("Staff.xml");
                    CreateNewTA(TaBox.Text);
                }
                else if (ChangeTa != "" && TaBox.Text == "") //means we need to cancel the ta of this course.
                {
                    int workload = 0;
                    XmlDocument minusWorkLoad = new XmlDocument();
                    XmlDocument update = new XmlDocument();
                    minusWorkLoad.Load("Staff.xml");
                    update.Load("Course.xml");
                    String PathForMinus = "//HeadTAID";
                    String PathUpdate = "//Subject";
                    XmlNodeList nodes = minusWorkLoad.SelectNodes(PathForMinus);
                    XmlNodeList nodesUpdate = update.SelectNodes(PathUpdate);
                    for (int j = 0; j < nodesUpdate.Count; j++)
                    {
                        if (nodesUpdate[j].ParentNode.ChildNodes[0].InnerText == CourseID)
                        {
                            nodesUpdate[j].ParentNode.ChildNodes[4].InnerText = "";
                            nodesUpdate[j].ParentNode.ChildNodes[4].Attributes[0].Value = "";
                        }
                    }
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        if (nodes[i].InnerText == ChangeTa)
                        {
                            int.TryParse(nodes[i].Attributes[1].Value, out workload);
                            workload = workload - 1;
                            nodes[i].Attributes[1].Value = workload.ToString();
                        }
                    }
                    update.Save("Course.xml");
                    minusWorkLoad.Save("Staff.xml");
                }
              /*  else if(Tabox2.Text==""&&TaBox.Text=="")
                {
                    MessageBox.Show("Both is no content");
                }*/
                //here is some other update need to be check if it also need to be conducted in the second Ta.
                if (ChangeTa2 == "" && Tabox2.Text != "")  //need to the second TA box. here, means, we provide a second Ta to that course.
                {
                    CreateNewTA2(Tabox2.Text);
                }
                if (ChangeTa2 != "" && Tabox2.Text != "" &&ChangeTa2!=Tabox2.Text) //means need to swap the secondTA into another secondTa.
                {
                    int workload;
                    XmlDocument minusWorkLoad = new XmlDocument();
                    minusWorkLoad.Load("Staff.xml");
                    String PathForMinus = "//HeadTAID";
                    XmlNodeList nodes = minusWorkLoad.SelectNodes(PathForMinus);
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        if (nodes[i].InnerText == ChangeTa2)
                        {
                            int.TryParse(nodes[i].Attributes[1].Value, out workload);
                            workload = workload - 1;
                            nodes[i].Attributes[1].Value = workload.ToString();
                        }
                    }                                
                    minusWorkLoad.Save("Staff.xml");
                    CreateNewTA2(Tabox2.Text);
                }
                if (ChangeTa2 != "" && Tabox2.Text == "") //Means need to cancel the second Ta.
                {
                    int workload=0;
                    XmlDocument minusWorkLoad = new XmlDocument();
                    XmlDocument update = new XmlDocument();
                    minusWorkLoad.Load("Staff.xml");
                    update.Load("Course.xml");
                    String PathForMinus = "//HeadTAID";
                    String PathUpdate = "//Subject";
                    XmlNodeList nodes = minusWorkLoad.SelectNodes(PathForMinus);
                    XmlNodeList nodesUpdate = update.SelectNodes(PathUpdate);
                    for (int j = 0; j < nodesUpdate.Count; j++)
                    {
                        if (nodesUpdate[j].ParentNode.ChildNodes[0].InnerText == CourseID)
                        {
                            nodesUpdate[j].ParentNode.ChildNodes[5].InnerText = "";
                            nodesUpdate[j].ParentNode.ChildNodes[5].Attributes[0].Value = "";
                        }
                    }
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        if (nodes[i].InnerText == ChangeTa2)
                        {
                            int.TryParse(nodes[i].Attributes[1].Value, out workload);
                            workload = workload - 1;
                            nodes[i].Attributes[1].Value = workload.ToString();
                        }
                    }
                    update.Save("Course.xml");
                    minusWorkLoad.Save("Staff.xml");
                }
            }
            else
            {
                MessageBox.Show("You have to put something in your course box", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        public DataTable checkTimeClash_Ta(String CourseID) //Here is for get the timetable for the course i need to choos a Ta for it. because i need to check if there has time clash of that Ta.       
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            String path = "//CourseID";
            XmlNodeList nodes = doc.SelectNodes(path);
            DataTable table = new DataTable();
            DataColumn col1 = new DataColumn("firstTime", typeof(string));
            DataColumn col2 = new DataColumn("secondTime", typeof(string));
            DataColumn col3 = new DataColumn("thirdTime", typeof(string));
            table.Columns.Add(col1);
            table.Columns.Add(col2);
            table.Columns.Add(col3);
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].InnerText == CourseID)
                {
                    DataRow dr = table.NewRow();
                    dr[0] = nodes[i].Attributes[0].Value.ToString();
                    dr[1] = nodes[i].Attributes[1].Value.ToString();
                    dr[2] = nodes[i].Attributes[2].Value.ToString();
                    table.Rows.Add(dr);
                }//get all the time information of that course and put them all into a table.
            }
            return table; //then return the time table of that course.
        }

        public Boolean TaOneTimeClash(DataTable table, String Ta) // here table is the table i get from the "checkTimeClash_Ta(String CourseID)" fucntion.
        {
            if (Ta == "")
            {
                return false;
            }
            if (Ta == ChangeTa)
            {
                return false;
            } // And then i need to get the whole time table of that Ta so that i can compare with the courseTimeTable. And i can check if there has time clash of that Ta.
            String firstTime = table.Rows[0][0].ToString();
            String secondTime = table.Rows[0][1].ToString();
            String thirdTime = table.Rows[0][2].ToString(); //here is the three time information of the course.
            if (firstTime == "" && secondTime == "" && thirdTime == "")
            {
                return false;
            }
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            String path = "//Courses/CourseInfo/Course/TAID1"; 
            XmlNodeList nodes = doc.SelectNodes(path);
            DataTable dt = new DataTable();
            DataColumn col1 = new DataColumn("firstTime", typeof(string));
            DataColumn col2 = new DataColumn("secondTime", typeof(string));
            DataColumn col3 = new DataColumn("thirdTime", typeof(string));
            dt.Columns.Add(col1);
            dt.Columns.Add(col2);
            dt.Columns.Add(col3);
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].InnerText == Ta)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = nodes[i].ParentNode.ChildNodes[0].Attributes[0].Value.ToString();
                    dr[1] = nodes[i].ParentNode.ChildNodes[0].Attributes[1].Value.ToString();
                    dr[2] = nodes[i].ParentNode.ChildNodes[0].Attributes[2].Value.ToString();
                    dt.Rows.Add(dr);
                }
            } //here i collect all course schedule of that ta into one table
            for(int i=0;i<dt.Rows.Count;i++)
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (firstTime == dt.Rows[i][j].ToString() || secondTime == dt.Rows[i][j].ToString() || thirdTime == dt.Rows[i][j].ToString())
                    {
                        return true;
                    }
                }  //then the comparation happens in this multiple for-loop
            //Same procedure happen to the TA2, too.
            XmlDocument xml = new XmlDocument();
            xml.Load("Course.xml");
            String path1 = "//Courses/CourseInfo/Course/TAID2";
            XmlNodeList nodes2 = xml.SelectNodes(path1);
            DataTable dt2 = new DataTable();
            DataColumn col12 = new DataColumn("firstTime", typeof(string));
            DataColumn col22 = new DataColumn("secondTime", typeof(string));
            DataColumn col32 = new DataColumn("thirdTime", typeof(string));
            dt2.Columns.Add(col12);
            dt2.Columns.Add(col22);
            dt2.Columns.Add(col32);
            for (int i = 0; i < nodes2.Count; i++)
            {
                if (nodes2[i].InnerText == Ta)
                {
                    DataRow dr2 = dt2.NewRow();
                    dr2[0] = nodes2[i].ParentNode.ChildNodes[0].Attributes[0].Value.ToString();
                    dr2[1] = nodes2[i].ParentNode.ChildNodes[0].Attributes[1].Value.ToString();
                    dr2[2] = nodes2[i].ParentNode.ChildNodes[0].Attributes[2].Value.ToString();
                    dt2.Rows.Add(dr2);
                }
            }
            for (int i = 0; i < dt2.Rows.Count; i++)
                for (int j = 0; j < dt2.Columns.Count; j++)
                {
                    if (firstTime == dt2.Rows[i][j].ToString() || secondTime == dt2.Rows[i][j].ToString() || thirdTime == dt2.Rows[i][j].ToString())
                    {
                        return true;
                    }
                }
            //Same 
            MessageBox.Show("Assigned successful", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return false;
        }

        public Boolean TaTwoTimeClash(DataTable table, String Ta) //As same as "TaOneTimeClash()" fucntion
        {
            if (Ta == "")
                return false;
            if (Ta == ChangeTa2)
                return false;
            String firstTime = table.Rows[0][0].ToString();
            String secondTime = table.Rows[0][1].ToString();
            String thirdTime = table.Rows[0][2].ToString();
            if (firstTime == "" && secondTime == "" && thirdTime == "")
            {
                return false;
            }
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            String path = "//Courses/CourseInfo/Course/TAID2";
            XmlNodeList nodes = doc.SelectNodes(path);
            DataTable dt = new DataTable();
            DataColumn col1 = new DataColumn("firstTime", typeof(string));
            DataColumn col2 = new DataColumn("secondTime", typeof(string));
            DataColumn col3 = new DataColumn("thirdTime", typeof(string));
            dt.Columns.Add(col1);
            dt.Columns.Add(col2);
            dt.Columns.Add(col3);
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].InnerText == Ta)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = nodes[i].ParentNode.ChildNodes[0].Attributes[0].Value.ToString();
                    dr[1] = nodes[i].ParentNode.ChildNodes[0].Attributes[1].Value.ToString();
                    dr[2] = nodes[i].ParentNode.ChildNodes[0].Attributes[2].Value.ToString();
                    dt.Rows.Add(dr);
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (firstTime == dt.Rows[i][j].ToString() || secondTime == dt.Rows[i][j].ToString() || thirdTime == dt.Rows[i][j].ToString())
                    {
                        return true;
                    }
                }
            XmlDocument xml = new XmlDocument();
            xml.Load("Course.xml");
            String path2 = "//Courses/CourseInfo/Course/TAID1";
            XmlNodeList nodes2 = xml.SelectNodes(path2);
            DataTable dt2 = new DataTable();
            DataColumn col12 = new DataColumn("firstTime", typeof(string));
            DataColumn col22= new DataColumn("secondTime", typeof(string));
            DataColumn col32 = new DataColumn("thirdTime", typeof(string));
            dt2.Columns.Add(col12);
            dt2.Columns.Add(col22);
            dt2.Columns.Add(col32);
            for (int i = 0; i < nodes2.Count; i++)
            {
                if (nodes[i].InnerText == Ta)
                {
                    DataRow dr2 = dt2.NewRow();
                    dr2[0] = nodes2[i].ParentNode.ChildNodes[0].Attributes[0].Value.ToString();
                    dr2[1] = nodes2[i].ParentNode.ChildNodes[0].Attributes[1].Value.ToString();
                    dr2[2] = nodes2[i].ParentNode.ChildNodes[0].Attributes[2].Value.ToString();
                    dt2.Rows.Add(dr2);
                }
            }
            for (int i = 0; i < dt2.Rows.Count; i++)
                for (int j = 0; j < dt2.Columns.Count; j++)
                {
                    if (firstTime == dt2.Rows[i][j].ToString() || secondTime == dt2.Rows[i][j].ToString() || thirdTime == dt2.Rows[i][j].ToString())
                    {
                        return true;
                    }
                }
            MessageBox.Show("Assigned successful", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return false;
        }

        private void okbutton_Click(object sender, EventArgs e)
        {
            DataTable table = checkTimeClash_Ta(CourseID);  // here i get the time table of the course i choose.
            if(Tabox2.Text==TaBox.Text&&(Tabox2.Text!=""&&TaBox.Text!=""))
            {
                MessageBox.Show("Duplication Happen", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }// check if Tabox1 and TaBox2 input same name.
            if(TaOneTimeClash(table,TaBox.Text))
            {
                MessageBox.Show("There are time clash with TaID : " + TaBox.Text, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            } //check if there has time clash for the First Ta.
            //And then check if there has time clash for the second Ta.
            if(TaTwoTimeClash(table,Tabox2.Text))
            {
                MessageBox.Show("There are time calsh with TaID : " + Tabox2.Text, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            addTa(); //if everything is OK, then i can add Ta.
            UpdateGridView(); // Then the gridView will update for showing latest information.
            UpdateComboList1();
            UpdataComboList2();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            String path = "//CourseID";
            XmlNodeList nodes = doc.SelectNodes(path);
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].InnerText == CourseID)
                {
                    if (nodes[i].Attributes[0].Value != "" && nodes[i].Attributes[1].Value != "" && nodes[i].Attributes[2].Value != "")
                    {
                        Tabox2.Text = comboBox2.SelectedValue.ToString();
                    }
                    else
                    {
                        MessageBox.Show("please input the time for the course first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        okbutton.Visible = false;
                        return;
                    }
                }
            }
        }

        //a function to merge cells
        public void Merge(Microsoft.Office.Interop.Excel.Range range, String value)
        {
            //To merge the cells
            range.Merge(range.MergeCells);
            //clear the value of the cell
            range.Value2 = Type.Missing;
            //write the data you want to input 
            range.Value2 = value;
            //make the font in the center of the cell
            range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            //draw the frame
            range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
            //the size of font
            range.Font.Size = 13;
        }
        //a function to export Excel
        public void ExportToExcel2(DataSet ds, String Name)
        {
            if (ds == null)
                return;

            //create excel, including workbooks and worksheet
            Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp2 = new Microsoft.Office.Interop.Excel.ApplicationClass();
            ExcelApp2.Application.Workbooks.Add(true);
            ExcelApp2.Application.Workbooks.Add(Type.Missing);
            Microsoft.Office.Interop.Excel.Worksheet Sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)ExcelApp2.Sheets[1];
            //create data table
            System.Data.DataTable dt = ds.Tables[0];
            System.Data.DataTable dt1 = ds.Tables[1];

            int countrow = 1;   //It is used to count the current row

            //title "Courses"
            Microsoft.Office.Interop.Excel.Range TitleRange1 = Sheet1.get_Range(Sheet1.Cells[1, 1], Sheet1.Cells[1, 4]);
            Merge(TitleRange1, "Courses");



            //the first table
            //get the range of cell
            var RowAll = Sheet1.get_Range(Sheet1.Cells[2, 1], Sheet1.Cells[(dt.Rows.Count) + 3, 4]);
            //make font stand in the middle of cell
            RowAll.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            //the font size
            RowAll.Font.Size = 11;
            //draw frame in the range area
            RowAll.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

            Microsoft.Office.Interop.Excel.Range range = Sheet1.Range[Sheet1.Cells[2, 1], Sheet1.Cells[2, 4]];
            range.Interior.ColorIndex = 37;
            countrow++;

            //this is the header of the first table
            for (int a = 0; a < dt.Columns.Count; a++)
            {
                ExcelApp2.Cells[2, a + 1] = dt.Columns[a].ColumnName;
            }
            countrow++;

            //title "Major Courses"
            //get the range of cell we need to merge
            Microsoft.Office.Interop.Excel.Range TitleRange2 = Sheet1.get_Range(Sheet1.Cells[3, 1], Sheet1.Cells[3, 4]);
            Merge(TitleRange2, "Major Courses");

            //this is the data of the first table
            for (int a = 0; a < dt.Rows.Count; a++)
            {
                for (int b = 0; b < dt.Columns.Count; b++)
                {
                    //write data from dataset one by one
                    ExcelApp2.Cells[a + 4, b + 1] = dt.Rows[a][b].ToString();
                }
                //plus 1 when finish writing a row
                countrow++;
            }


            //title "GE Required Courses"
            //get the range of cell we need to merge
            Microsoft.Office.Interop.Excel.Range TitleRange3 = Sheet1.get_Range(Sheet1.Cells[countrow + 1, 1], Sheet1.Cells[countrow + 1, 4]);
            //call the Merge function
            Merge(TitleRange3, "GE Required Courses");

            //the second table
            //their functions are same as we told before
            var RowAll2 = Sheet1.get_Range(Sheet1.Cells[countrow + 2, 1], Sheet1.Cells[countrow + (dt1.Rows.Count) + 1, 4]);
            RowAll2.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            RowAll2.Font.Size = 11;
            RowAll2.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

            //the data of the second table
            for (int a = 0; a < dt1.Rows.Count; a++)
            {

                for (int b = 0; b < dt1.Columns.Count; b++)
                {
                    ExcelApp2.Cells[countrow + 2, b + 1] = dt1.Rows[a][b].ToString();
                }
                countrow++;
            }


            ExcelApp2.Cells.EntireColumn.AutoFit();  //make the cell fit to the font
            ExcelApp2.Cells.Font.Name = "Georgia";   //the font

            try
            {
                //never display the alerts when it covers the previous file
                ExcelApp2.DisplayAlerts = false;
                //save
                ExcelApp2.ActiveWorkbook.Saved = false;
                ExcelApp2.ActiveWorkbook.SaveAs(Name, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel5, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                MessageBox.Show("create TeachingAssignmentWithTA.xls success", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                //show the error, for example, the file is openning, then it can not be saved
                MessageBox.Show("error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                ExcelApp2.Quit();

            }
        }

        private void exportbut_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            //read the xml
            doc.Load("Course.xml");
            String path4 = "//Courses//CourseInfo/Course";
            //this is the name and path to save this file
            String fileName2 = "D:\\TeachingAssignmentWithTA.xls";
            //create many data column, this is the header of the data table
            System.Data.DataTable dt4 = new System.Data.DataTable();
            DataColumn c1 = new DataColumn("Subject", typeof(string));
            DataColumn c2 = new DataColumn("Instructor", typeof(string));
            DataColumn c3 = new DataColumn("TA", typeof(string));
            DataColumn c4 = new DataColumn("Students", typeof(string));
            //add them into the data table
            dt4.Columns.Add(c1);
            dt4.Columns.Add(c2);
            dt4.Columns.Add(c3);
            dt4.Columns.Add(c4);

            //create a data table
            System.Data.DataTable dt5 = new System.Data.DataTable();
            //create many data column, this is the header of the data table
            DataColumn i1 = new DataColumn("Subject", typeof(string));
            DataColumn i2 = new DataColumn("Instructor", typeof(string));
            DataColumn i3 = new DataColumn("TA", typeof(string));
            DataColumn i4 = new DataColumn("Students", typeof(string));
            //add them into the data table
            dt5.Columns.Add(i1);
            dt5.Columns.Add(i2);
            dt5.Columns.Add(i3);
            dt5.Columns.Add(i4);

            //create a list to save data that read from xml
            XmlNodeList ta1 = doc.SelectNodes(path4);
            for (int i = 0; i < ta1.Count; i++)
            {
                //if the course category is GE
                if (ta1[i].ChildNodes[2].InnerText.ToString() == "GE Course")
                {
                    DataRow dr5 = dt5.NewRow();
                    dr5[0] = ta1[i].ChildNodes[1].InnerText.ToString();
                    dr5[1] = ta1[i].ChildNodes[6].Attributes[0].Value.ToString();
                    dr5[3] = ta1[i].ChildNodes[3].InnerText;
                    //to see whether the second TA exist or not
                    if (ta1[i].ChildNodes[5].Attributes[0].Value == "")
                    {
                        //there is one TA
                        dr5[2] = ta1[i].ChildNodes[4].Attributes[0].Value.ToString();
                    }
                    else
                    {
                        //if there are two TA, then add them together 
                        dr5[2] = ta1[i].ChildNodes[4].Attributes[0].Value.ToString() + " / " + ta1[i].ChildNodes[5].Attributes[0].Value.ToString();
                    }
                    //add a data row to data table
                    dt5.Rows.Add(dr5);
                }

                 //if the course category is Major Course
                else if (ta1[i].ChildNodes[2].InnerText.ToString() == "Major Course")
                {
                    //create data row to save different form a row
                    DataRow dr4 = dt4.NewRow();
                    //these are the data we need to show on Excel
                    dr4[0] = ta1[i].ChildNodes[1].InnerText;
                    dr4[1] = ta1[i].ChildNodes[6].Attributes[0].Value.ToString();
                    dr4[3] = ta1[i].ChildNodes[3].InnerText;
                    if (ta1[i].ChildNodes[5].Attributes[0].Value == "")
                    {
                        //there is one TA
                        dr4[2] = ta1[i].ChildNodes[4].Attributes[0].Value.ToString();
                    }
                    else
                    {
                        //if there are two TA, then add them together
                        dr4[2] = ta1[i].ChildNodes[4].Attributes[0].Value.ToString() + " / " + ta1[i].ChildNodes[5].Attributes[0].Value.ToString();
                    }
                    //add a data row to data table
                    dt4.Rows.Add(dr4);
                }
                else
                {

                }
            }
            //create a dataset to save those data table
            DataSet ds2 = new DataSet();
            ds2.Tables.Add(dt4);
            ds2.Tables.Add(dt5);
            //call the function to display data in Excel
            ExportToExcel2(ds2, fileName2);
            // Pass the dataset and the file position into this function.
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void tAArrangementToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void CourseBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FormHeadPixel headPixel = new FormHeadPixel();
            headPixel.Show();
            this.Close();
        }

           
    }
}
